package com.grm.controller;

import com.grm.common.Result;
import com.grm.entity.LoginRequest;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * test
 *
 * @author gaorimao
 * @since 2021-04-29
 */
@RestController
@Validated
public class TestController {
    /**
     * get请求，拼在路径上的参数校验，需要在控制器上添加@Validated注解
     */
    @GetMapping("/test/get/params")
    public Result testUsername(@Length(min = 4,max=20) @RequestParam("username") String username,
        @Length(min = 4,max=20) @RequestParam("password") String password) {
        return Result.success();
    }

    /**
     * 将校验结果放进BindingResult里面,用户自行判断并处理
     */
    @PostMapping(value = "/test/params", produces = "application/json;charset=UTF-8")
    public Result testBindingResult(@RequestBody @Valid LoginRequest loginRequest) {
        return Result.success();
    }
}